<template>
	<basic-container>
		<el-tabs v-model="activeName" @tab-click="handleClick">
			<el-tab-pane label="平台优惠券" name="first">
				<avue-crud :option="optionP" :data="data" ref="formRef" v-model="form" :table-loading="loading" :permission="permissionList" :page="page" @refresh-change="refreshChange" @search-change="searchChange" @search-reset="searchReset" @current-change="currentChange" @size-change="sizeChange" @on-load="onLoad">
					<!-- 新增按钮  -->
					<template slot="menuLeft"  v-if="activeName=='first'">
						<el-button type="primary" size="mini" @click="addCoupon" v-has="'/mall-cloud-cards/coupon/addItem'">新增优惠券</el-button>
					</template>
					<template slot-scope="scope" slot="menu">
						<!-- <el-button icon="el-icon-edit" size="small" v-if="activeName=='first' && scope.row.couponGetType=='3'" type="text" @click="grantShow(scope.row)" v-has="'/mall-cloud-cards/coupon/couponSendSave'">发放</el-button> -->
		<!--				<el-button icon="el-icon-edit" size="small" v-if="activeName=='first'" type="text" @click="editShow(scope.row)" v-has="'/mall-cloud-cards/coupon/editItem'">编辑</el-button>-->
						<!--<el-button icon="el-icon-delete" size="small" v-if="activeName=='first'" type="text" @click="rowDel(scope.row)" v-has="'/mall-cloud-cards/coupon/delete'">删除</el-button>-->
						<el-button icon="el-icon-view" size="small" type="text" @click="detailShow(scope.row)" v-has="'/mall-cloud-cards/couponInfo/list'">查看详情</el-button>
						<el-button icon="el-icon-view" size="small" v-if="activeName=='second'" type="text" @click="goodsDetailShow(scope.row)" v-has="'/mall-cloud-cards/couponGoods/list'">商品</el-button>
					</template>
					<template slot-scope="scope" slot="dateScope">
						<el-tag color="#fff" style="border: none;color: #333333;">{{scope.row.couponBeginTime}}~{{scope.row.couponEndTime}}</el-tag>
					</template>
					<template slot="search">
						<el-col :span="12">
							<el-form-item label="使用时间" style="display: inline-block;width: 50%;">
								<el-date-picker placeholder="开始时间" @change="getTime" value-format="yyyy-MM-dd" v-model="couponBeginTime" type="date" size="small">
								</el-date-picker>
							</el-form-item>
							<el-form-item label="至" label-width="40px" style="display: inline-block;width: 50%;">
								<el-date-picker placeholder="结束日期" @change="getTime" value-format="yyyy-MM-dd" v-model="couponEndTime" type="date" size="small">
								</el-date-picker>
							</el-form-item>
						</el-col>
					</template>
					<template slot-scope="{ row }" slot="couponStatus">
						<el-switch @change="rowSwitchChange(row)" v-model="row.couponStatus"></el-switch>
					</template>
				</avue-crud>
			</el-tab-pane>
			<el-tab-pane label="店铺优惠券" name="second">
				<avue-crud :option="optionD" :data="data" ref="formRef" v-model="form" :table-loading="loading" :permission="permissionList" :page="page" @refresh-change="refreshChange" @search-change="searchChange" @search-reset="searchReset" @current-change="currentChange" @size-change="sizeChange" @on-load="onLoad">
					<!-- 新增按钮  -->
					<template slot="menuLeft"  v-if="activeName=='first'">
						<el-button type="primary" size="mini" @click="addCoupon" v-has="'/mall-cloud-cards/coupon/addItem'">新增优惠券</el-button>
					</template>
					<template slot-scope="scope" slot="menu">
						<!-- <el-button icon="el-icon-edit" size="small" v-if="activeName=='first' && scope.row.couponGetType=='3'" type="text" @click="grantShow(scope.row)" v-has="'/mall-cloud-cards/coupon/couponSendSave'">发放</el-button> -->
		<!--				<el-button icon="el-icon-edit" size="small" v-if="activeName=='first'" type="text" @click="editShow(scope.row)" v-has="'/mall-cloud-cards/coupon/editItem'">编辑</el-button>-->
						<!--<el-button icon="el-icon-delete" size="small" v-if="activeName=='first'" type="text" @click="rowDel(scope.row)" v-has="'/mall-cloud-cards/coupon/delete'">删除</el-button>-->
						<el-button icon="el-icon-view" size="small" type="text" @click="detailShow(scope.row)" v-has="'/mall-cloud-cards/couponInfo/list'">查看详情</el-button>
						<el-button icon="el-icon-view" size="small" v-if="activeName=='second'" type="text" @click="goodsDetailShow(scope.row)" v-has="'/mall-cloud-cards/couponGoods/list'">商品</el-button>
					</template>
					<template slot-scope="scope" slot="dateScope">
						<el-tag color="#fff" style="border: none;color: #333333;">{{scope.row.couponBeginTime}}~{{scope.row.couponEndTime}}</el-tag>
					</template>
					<template slot="search">
						<el-col :span="12">
							<el-form-item label="使用时间" style="display: inline-block;width: 50%;">
								<el-date-picker placeholder="开始时间" @change="getTime" value-format="yyyy-MM-dd" v-model="couponBeginTime" type="date" size="small">
								</el-date-picker>
							</el-form-item>
							<el-form-item label="至" label-width="40px" style="display: inline-block;width: 50%;">
								<el-date-picker placeholder="结束日期" @change="getTime" value-format="yyyy-MM-dd" v-model="couponEndTime" type="date" size="small">
								</el-date-picker>
							</el-form-item>
						</el-col>
					</template>
					<template slot-scope="{ row }" slot="couponStatus">
						<el-switch @change="rowSwitchChange(row)" v-model="row.couponStatus"></el-switch>
					</template>
				</avue-crud>
			</el-tab-pane>
		</el-tabs>
		
		<!-- 新增按钮弹框 -->
		<el-dialog :title="title" :visible.sync="dialogVisible" width="60%" :close-on-click-modal="false" :before-close="closeDialog">
			<el-form :model="addForm" :rules="rules" ref="addForm" label-width="120px" class="demo-ruleForm">
				<el-form-item label="优惠券名称:" prop="couponName" style="width: 50%;display: inline-block;">
					<el-input :disabled="isView"  v-model="addForm.couponName" placeholder="请输入优惠券名称"></el-input>
				</el-form-item>
				<el-form-item label="优惠券数量:" prop="couponCount" style="width: 50%;display: inline-block;">
					<el-tooltip class="item" effect="dark" content="数量为空或0时表示不限制数量" placement="top-start">
						<el-input-number :min="0" :max="100000000"  v-model="addForm.couponCount" placeholder="0"></el-input-number>
					</el-tooltip>
				</el-form-item>
				<el-form-item label="优惠券金额:" prop="couponAmount" style="width: 50%;display: inline-block;">
					<el-input-number :min="0" :max="10000" @blur="checkCouponPrice" v-model="addForm.couponAmount" placeholder="0"></el-input-number>
				</el-form-item>
				<el-form-item label="可用订单金额:" prop="couponOrderAmount" style="width: 50%;display: inline-block;">
					<el-tooltip class="item" effect="dark" content="订单金额达到该数值才可使用此优惠券(金额为0或不填时默认为代金券)" placement="top-start">
						<el-input-number :min="0" :max="100000000" @blur="checkOrderPrice" v-model="addForm.couponOrderAmount" placeholder="0"></el-input-number>
					</el-tooltip>
				</el-form-item>
				<el-form-item label="开始使用时间:" prop="couponBeginTime" style="width: 50%;display: inline-block;margin-top: 10px;">
					<el-date-picker :readonly="isView" @change="checkStartTime" :picker-options="pickerOptions" v-model="addForm.couponBeginTime" value-format="yyyy-MM-dd" type="date" placeholder="请选择开始使用时间" style="width: 100%;"></el-date-picker>
					<!-- <el-date-picker v-model="addForm.dateScope" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 100%;"></el-date-picker> -->
				</el-form-item>
				<el-form-item label="结束使用时间:" prop="couponEndTime" style="width: 50%;display: inline-block;margin-top: 10px;">
					<el-date-picker :readonly="isView" @change="getAddTime" :picker-options="pickerOptions" v-model="addForm.couponEndTime" value-format="yyyy-MM-dd" type="date" placeholder="请选择结束使用时间" style="width: 100%;"></el-date-picker>
				</el-form-item>
				<el-form-item label="领取方式:" prop="couponGetType">
					<el-select :disabled="isView" v-model="addForm.couponGetType" placeholder="请选择领取方式" style="width: 100%;">
<!--						<el-option label="平台手动发放" value="3"></el-option>-->
						<el-option label="自助领取" value="1"></el-option>
						<!--<el-option label="红包雨优惠券" value="6"></el-option>-->
					</el-select>
				</el-form-item>
				<el-form-item label="优惠券描述:" prop="couponDesc">
					<el-input :disabled="isView" type="textarea" maxlength="50" show-word-limit v-model="addForm.couponDesc"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button v-if="!isView" @click="closeDialog('addForm')">取 消</el-button>
				<el-button  v-if="!isView"  type="primary" @click="addRecurseData('addForm')" :loading="addLoad" v-has="'/mall-cloud-cards/coupon/addItem'">保 存</el-button>
				<el-button v-else @click="closeDialog">关闭</el-button>
			</span>
		</el-dialog>
	<!--		平台优惠详情-->
		<el-drawer title="优惠券详情" @close="closeDrawer" :visible.sync="drawerD" size="80%">
            <el-table border :data="tableData"  style="width: 100%" >
				<el-table-column prop="couponName" align="center" label="优惠券名称" width="180">
				</el-table-column>
				<el-table-column prop="userName" align="center" label="用户昵称" width="180">
				</el-table-column>
				<el-table-column prop="moblie"  align="center" label="手机号码">
				</el-table-column>
				<el-table-column prop="couponAmount" align="center" label="优惠券金额">
				</el-table-column>
				<el-table-column prop="useTime" align="center" label="使用时间">
				</el-table-column>
				<el-table-column prop="status" align="center" label="是否使用">
					<template slot-scope="scope">
						{{scope.row.status | status}}
					</template>
				</el-table-column>
            </el-table>
            <div style="margin: 50px auto;">
              <div style="float: right;">
                  <el-pagination
                          @size-change="handleSizeChange"
                          @current-change="handleCurrentChange"
                          :current-page="pageCurrent"
                          :page-sizes="[10, 20, 30, 40]"
                          :page-size="pageSize"
                          layout="total, sizes, prev, pager, next, jumper"
                          :total="total">
                  </el-pagination>
              </div>
            </div>
        </el-drawer>
<!--	发放优惠券 暂时没用	-->
		<el-drawer title="优惠券信息" :visible.sync="grant" size="60%" :with-header="false" @close="isGrant('dynamicValidateForm')">
            <div style="margin: 10px 20px;">
                <el-col><p style="font-size: 16px;font-weight: 700;">面值: {{grantData.couponAmount}}元</p></el-col>
                <el-col><p style="font-size: 16px;font-weight: 700;">可使用订单金额: {{grantData.couponOrderAmount}}元</p></el-col>
                <el-col><p style="font-size: 16px;font-weight: 700;">优惠券数量: {{grantData.couponCount}}张</p></el-col>
                <el-col><p style="font-size: 16px;font-weight: 700;"> 有效期： {{grantData.couponBeginTime}} ~ {{grantData.couponEndTime}}</p></el-col>
                <div style="margin: 10px 0px">
                    <el-col ><p style="font-size: 16px;font-weight: 700;">发送对象:</p></el-col>
                    <el-radio-group v-model="radio" @change="radioChange">
                        <el-radio :label="1">所有会员</el-radio>
                        <el-radio :label="2">指定会员</el-radio>
                    </el-radio-group>
                    <!--				-->
                    <div v-if="this.radio==2" >
                        <el-col style="margin: 10px 0px"><p><span style="font-size: 14px;font-weight: 700;"> 会员列表 </span></p></el-col>
                        <el-form  :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
                            <el-form-item
                                    v-for="(domain, index) in dynamicValidateForm.domains"
                                    :label="'会员' + (index+1)"
                                    :key="domain.key"
                                    :prop="'domains.' + index + '.value'"
                                    :rules="{required: true, message: '会员名不能为空', trigger: 'blur'}"
                                    >
                                <el-input v-model="domain.value"  @input="getuser(domain.value)"></el-input>
                                <el-button @click.prevent="removeDomain(domain)" size="mini" >删除</el-button>
                            </el-form-item>
                            <el-form-item>
                                <el-button @click="addDomain" size="mini" type="primary" >新增</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                    <!--  提交按钮-->
                    <div style="margin-top: 10px">
                        <el-button type="primary" size="mini" @click="grantBtn" v-has="'/mall-cloud-cards/coupon/couponSendSave'" :disabled="btnEnable">确定</el-button>
                    </div>
                </div>

            </div>
		</el-drawer>
		<!--	点击商品-->
		<el-drawer title="参与商品" :visible.sync="dialogVisibleC" size="70%" :with-header="false">
			<!--			表单-->
			<div style="padding: 0 50px;">
				<avue-crud :data="dataC" :option="optionC"  :page="pageC" @size-change="sizeChangeC" @current-change="currentChangeC"  >
				</avue-crud>
			</div>
		</el-drawer>
	</basic-container>
</template>
<script>
	import {
		getList,
		remove,
		update,
		add,
        getCouponD,
        getInfo,
        sendSave,
		getGoodsList,
		getlistManageByAccount,
		updateDisplay
	} from "@/api/base/operations/coupon";
	import {validatNum,validatPrice} from '@/util/validate.js'
	import {isInteger,testPirceTenThousand,testPirceZero} from '@/util/util.js'
	import {
		mapGetters
	} from "vuex";
	import StoreTemplate from "../store/storeTemplate";
	export default {
		components: {
			StoreTemplate
		},
		filters:{
			status(status){
				if(status==1){
					return '已使用'
				}else{
					return '未使用'
				}
			}
		},
		data() {
			return {
				pickerOptions: {
					disabledDate(time) {
						return time.getTime() + 3600 * 1000 * 24 * 1 < Date.now();
					},
				},
				iscouponAmount:false,
				getGoosListRow:{},
				pageC: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				tabRow:{},
                grantData:{},
				addLoad:false,
				dynamicValidateForm: {
					domains: [{
						value: ''
					}],
				},
				radio:1,
				btnEnable:false,
				grant:false,
                pageSize:10,
                pageCurrent:1,
                total:0,
                tableData:[],
				drawerD:false,
				title:'新增优惠券',
				dialogVisibleCD:false,
				dialogVisibleC:false,
				optionC:{
					menu:false,
					size:'mini',
					excelBtn: false,
					addBtn: false,
					cancelBtn: false,
					tip: false,
					index: false,
					selection: false,
					viewBtn: false,
					delBtn: false,
					editBtn: false,
					rowKey: 'id',
					height: 'auto',
					border:true,
					align:'center',
					menuWidth: 160,
					column: [
						{
							label: "商品名称",
							prop: "goodsName",

						},{
							label: "店铺名称",
							prop: "storeName",
						}]
				},
				dataC:[],
				couponEndTime: "",
				couponBeginTime: "",
				isView:false,
				//新增按钮弹框内容
				addForm: {
					couponName: "",
					// dateScope: "",
					couponEndTime: "",
					couponBeginTime: "",
					couponCount: "",
					couponAmount: "",
					couponOrderAmount: "",
					couponGetType: "",
					couponDesc: "",
				},
				rules: {
					couponName: [{
							required: true,
							message: "优惠券名称不能为空",
							trigger: "blur"
						},
						{
							min: 2,
							max: 18,
							message: "长度在 2 到 18 个字符",
							trigger: "blur"
						}
					],
					/*couponBeginTime: [{
						type:'date',
						required: true,
						message: "请选择开始使用时间",
						trigger: "blur"
					}, ],
					couponEndTime: [{
						type:'date',
						required: true,
						message: "请选择结束使用时间",
						trigger: "blur"
					}, ],*/
					// couponCount: [{
					// 	required: true,
					// 	// message: "请设定优惠券数量",
					// 	trigger: "blur"
					// }, {
					// 	validator: isInteger,
					// 	trigger: "blur",
					// 	}],

					couponAmount: [
							{
						required: true,
						message: "请设定优惠券金额",
						trigger: "blur"
						},{
							validator: testPirceTenThousand,
							trigger: "blur",
							}
					],
					couponOrderAmount: [{
						required: true,
						message: "请填写可用订单金额",
						trigger: "blur"
					},
						{
							validator: testPirceZero,
							trigger: "blur",
						}
					],
					couponGetType: [{
						required: true,
						message: "请选择领取方式",
						trigger: "blur"
					}, ],
					couponDesc: [
						{
							min: 0,
							max: 50,
							message: "请输入50字以内的优惠券描述",
							trigger: "change"
						},
					],
				},
				dialogVisible: false,
				form: {

				},
				query: {
					couponType:2
				},
				activeName: "first",
				loading: true,
				rolesTree: [],
				rolesKeys: [],
				box: false,
				page: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				option: {
					size:'mini',
					excelBtn: false,
					addBtn: false,
					searchBtn: true,
					cancelBtn: false,
					tip: false,
					index: false,
					border: true,
					selection: false,
					viewBtn: false,
					delBtn: false,
					editBtn: false,
					rowKey: 'id',
					height: 'auto',
					// calcHeight: 380,
					dialogType: 'dialog',
					align: 'center',
					menuWidth: 180,
					searchMenuSpan:6,
					column: [
						{
							label: "优惠劵名",
							prop: "couponName",
							width: 180,
							search: true
						},
						{
							label: "优惠券金额",
							prop: "couponAmount",
							width: 120,
						},
						{
							label: "可用订单金额",
							prop: "couponOrderAmount",
							width: 120,
						},
						{
							label: "使用时间",
							width:170,
							prop: "dateScope",
							startPlaceholder: '日期开始范围自定义',
							endPlaceholder: '日期结束范围自定义',
							slot: true,
							formslot: true,
						},
						// {
						// 	label: " 申请人 ",
						// 	prop: "owner",
						//
						// },
						{
							label: " 操作时间 ",
							width:170,
							prop: "addTime",
						},
						{
							label: " 操作人 ",
							prop: "owner",
						},
						{
							label: "发行数量",
							width: 120,
							prop: "couponCount",
						},
						{
							label: "已领取数量 ",
							width: 120,
							prop: "couponUsed",
						},
						{
							label: " 已使用数量 ",
							width: 120,
							prop: "couponIssueCount",
						},
						{
							label: " 是否开启 ",
							width: 100,
							prop: "couponStatus",
							slot: true
						}
					]
				},

				optionP: {
					size:'mini',
					excelBtn: false,
					addBtn: false,
					searchBtn: true,
					cancelBtn: false,
					tip: false,
					index: false,
					border: true,
					selection: false,
					viewBtn: false,
					delBtn: false,
					editBtn: false,
					rowKey: 'id',
					height: 'auto',
					// calcHeight: 380,
					dialogType: 'dialog',
					align: 'center',
					menuWidth: 200,
					searchMenuSpan:6,
					column: [
						{
							label: "优惠劵名",
							prop: "couponName",
							search: true,
							fixed:true,
							width:180,
						},
						{
							label: "优惠券金额",
							prop: "couponAmount",
							width: 120,
						},
						{
							label: "可用订单金额",
							prop: "couponOrderAmount",
							width: 120,
						},
						{
							label: "使用时间",
							prop: "dateScope",
							width:180,
							startPlaceholder: '日期开始范围自定义',
							endPlaceholder: '日期结束范围自定义',
							slot: true,
							formslot: true,
						},{
							label: " 操作时间 ",
							width: 150,
							prop: "addTime",
						},{
							label: " 操作人 ",
							prop: "owner",
						},
						{
							label: "发行数量",
							width: 120,
							prop: "couponCount",
						},
						{
							label: "已领取数量 ",
							width: 120,
							prop: "couponUsed",
						},
						{
							label: " 已使用数量 ",
							width: 100,
							prop: "couponIssueCount",
						},
						{
							label: " 是否开启 ",
							width: 100,
							prop: "couponStatus",
							slot: true
						}
					]
				},
				optionD: {
					size:'mini',
					excelBtn: false,
					addBtn: false,
					searchBtn: true,
					cancelBtn: false,
					tip: false,
					index: false,
					border: true,
					selection: false,
					viewBtn: false,
					delBtn: false,
					editBtn: false,
					rowKey: 'id',
					height: 'auto',
					// calcHeight: 380,
					dialogType: 'dialog',
					align: 'center',
					menuWidth: 200,
					searchMenuSpan:6,
					column: [
						{
							label: "优惠劵名",
							prop: "couponName",
							search: true,
							width:180,
						},
						{
							label: "优惠券金额",
							prop: "couponAmount",
							width: 120,
						},
						{
							label: "可用订单金额",
							prop: "couponOrderAmount",
							width: 120,
						},
						{
							label: "使用时间",
							prop: "dateScope",
							width:180,
							startPlaceholder: '日期开始范围自定义',
							endPlaceholder: '日期结束范围自定义',
							slot: true,
							formslot: true,
						},
						// {
						// 	label: " 申请人 ",
						// 	// width:150,
						// 	prop: "owner",
						// },
						{
							label: " 操作时间 ",
							width:150,
							prop: "addTime",
						},{
							label: " 操作人 ",
							prop: "owner",
						},
						{
							label: "发行数量",
							width: 120,
							prop: "couponCount",
						},
						{
							label: "已领取数量 ",
							width: 120,
							prop: "couponUsed",
						},
						{
							label: " 已使用数量 ",
							width: 120,
							prop: "couponIssueCount",
						},
					]
				},
				data:[],
				// data:[{
				// 	couponName:'双12',
				// 	couponAmount:'5',
				// 	couponOrderAmount:'50',
				// 	couponBeginTime:'2019-12-11',
				// 	couponEndTime:'2019-12-18',
				// 	owner:'admin',
				// 	couponCount:'100',
				// 	couponUsed:'2',
				// 	couponIssueCount:'1'
				// },{
				// 	couponName:'圣诞节',
				// 	couponAmount:'5',
				// 	couponOrderAmount:'50',
				// 	couponBeginTime:'2019-12-20',
				// 	couponEndTime:'2019-12-26',
				// 	owner:'admin',
				// 	couponCount:'100',
				// 	couponUsed:'4',
				// 	couponIssueCount:'0'
				// }]
				couponFlag: true
			};
		},
		computed: {
			...mapGetters(["permission", 'userInfo']),
			permissionList() {
				return {

				};
			},
		},
		watch:{
			dialogVisible(val){
				if(!val){
					this.$nextTick(()=>{
						this.$refs["addForm"].resetFields();
						this.addLoad=false
					})
				}
			},
		},
		//tabs切换
		created() {
			this.type = this.option.column[0];
		},
		methods: {
			isGrant(formName){
				this.$refs[formName].resetFields();
			},
			sizeChangeC(val) {
				this.pageC.pageSize = val
				this.getGoodsRow()
			},
			currentChangeC(val) {
				this.pageC.currentPage = val
				this.getGoodsRow()
			},
			radioChange(){
				this.btnEnable = false;
			},

		    //判断是否存在该会员
            getuser(val){
				getlistManageByAccount(val).then(res=>{
					console.log(res)
					if(res.data.data==""){
						this.$message({
							message:'会员不存在',
							type:'error'
						})
						this.btnEnable = true;

					}else {
						this.btnEnable = false;
					}
				})
            },
		    //发放优惠券
            grantBtn(){
				let id=this.grantData.id
				let type=this.radio
				if(type==1){
					sendSave(id,type).then(res=>{
						if(res.status==200){
							this.$message({
								message:'发放成功',
								type:'success'
							})
							this.grant=false
						}
					})
				}else{
					if(this.btnEnable){
						let Arr=[]
						for(let item of this.dynamicValidateForm.domains){
							Arr.push(item.value)
						}
						let users=Arr
						if(users[0]==""){
							this.$message({
								message:'会员名称不可为空',
								type:'error'
							})
							return
						}
						if(users.length!=0){
							sendSave(id,type,users).then(res=>{
								if(res.status==200){
									this.$message({
										message:'发放成功',
										type:'success'
									})
									this.grant=false
								}
							})
						}else{
							this.$message({
								message:'请输入指定会员',
								type:'success'
							})
						}
					}

				}
            },
            //发放优惠券
            grantShow(row) {
                this.grantData=row
                this.grant=true
                this.radio=1
            },
			removeDomain(item) {
				var index = this.dynamicValidateForm.domains.indexOf(item)
				if (index !== -1) {
					this.dynamicValidateForm.domains.splice(index, 1)
				}
				this.btnEnable = false;
			},
			addDomain() {
				this.dynamicValidateForm.domains.push({
					value: '',
				});
				this.btnEnable = false;
			},
			closeDrawer() {
				this.pageCurrent = 1;
				this.pageSize = 10;
			},
            handleSizeChange(val) {

				this.pageSize=val
				this.getTabRow()
            },
            handleCurrentChange(val) {
				this.pageCurrent=val
				this.getTabRow()
            },
			getDetails(row){
				console.log('商品点击详情')
				this.dialogVisibleCD=true
			},
			//根据优惠id获取优惠商品呢
			getGoodsRow(){
				let obj={}
				obj.couponId=this.getGoosListRow.id
				getGoodsList(this.pageC.currentPage,this.pageC.pageSize,obj).then(res=>{
					console.log(res)
					if(res.status==200){
						this.dataC=res.data.data
						this.pageC.total=res.data.count
					}
				})
			},
			goodsDetailShow(row){
            	this.getGoosListRow=row
				this.dialogVisibleC=true
				this.getGoodsRow()
			},
			//搜索时间范围限制
			getTime() {
				console.log('开始时间:',this.couponBeginTime,'结束时间:',this.couponEndTime)
				let nowDate=new Date().toLocaleDateString().replace(/\//g, "-")
				if(new Date(this.couponBeginTime) < new Date(nowDate)) {
					this.couponBeginTime =''
					return (this.$message({
						type: "error",
						message: "开始时间不能小于当前时间"
					}))
				}
				if(new Date(this.couponEndTime) < new Date(nowDate)) {
					this.couponEndTime=''
					return (this.$message({
						type: "error",
						message: "结束时间不能小于当前时间"
					}))
				}
				if (this.couponBeginTime > this.couponEndTime && this.couponBeginTime && this.couponEndTime) {
					this.$message({
						type: "error",
						message: "结束时间不能小于开始时间"
					});
					this.couponEndTime = ''
				}
			},

			//获取优惠券详情
			getTabRow(){
				let obj={}
				obj.couponId=this.tabRow.id
				getInfo(this.pageCurrent,this.pageSize,obj).then(res=>{
					console.log(res)
					if(res.status==200){
						this.tableData=res.data.data
						this.total=res.data.count
					}
				})
			},
			//查看优惠券详情
			detailShow(row) {
				this.tabRow=row
				this.drawerD=true
				this.getTabRow()

			},

			// 点击tab页跳转
			handleClick(tab, event) {
				this.data=[]
				this.$refs.formRef.searchReset()
				if(this.activeName=='first'){
					//平台优惠券
					this.query.couponType=2
					this.$refs.formRef.init();
					this.$nextTick(() => {
						// this.option=this.optionP
						this.$forceUpdate();
					})
				}else{
					//店铺优惠券
					this.query.couponType=1
					this.$refs.formRef.init();
					this.$nextTick(() => {
						// this.option=this.optionD
						this.$forceUpdate();
					})
				}
				this.page.currentPage = 1;
				this.onLoad(this.page, this.query);
				console.log(this.activeName)
				console.log(tab, event);
			},
			// 新增或者编辑 弹框 弹窗保存发放
			addRecurseData(addForm) {
				console.log(this.addForm)
				this.addLoad=true;
				// this.$refs[addForm].validate((valid) => {
				// 	console.log("55555")
					// if (valid) {
						// console.log(valid)
						//防重提交
						if (!this.addForm.couponName || this.addForm.couponName.length > 18 || this.addForm.couponName.length < 2) {
							this.$message({
								type: "error",
								message: "优惠劵名称长度在 2 到 18 个字符!"
							});
							this.onLoad(this.page);
							this.addLoad=false;
							return;
						}
						if(this.addForm.couponAmount&&!validatPrice(this.addForm.couponAmount) || this.addForm.couponAmount==0){
							this.$message({
								type: "error",
								message: "优惠券金额必须是0.01-10000.00之间的数字(保留两位小数)"
							});
							this.addLoad=false;
							return;
						}
						if (this.addForm.couponCount && !validatNum(this.addForm.couponCount) && this.addForm.couponCount != 0) {
							this.$message({
								type: "error",
								message: "优惠劵数量必须为正整数"
							});
							this.addLoad = false;
							return;
						}

						if(this.addForm.couponAmount >10000){
							this.$message({
								type: "error",
								message: "优惠券金额不能大于10000"
							});
							this.addLoad=false;
							return;
						}
						if(this.iscouponAmount){
							this.$message({
								type: "error",
								message: "优惠券金额不能大于订单可用金额"
							});
							this.addLoad=false;
							return;
						}
						if (!this.addForm.couponName ||  !this.addForm.couponAmount || !this.addForm.couponBeginTime || !this.addForm.couponEndTime || !this.addForm.couponGetType) {
							this.$message({
								type: "error",
								message: "请填写优惠券必要信息!"
							});
							this.addLoad=false;
							return
						}
						if(!this.addForm.couponCount){
							this.addForm.couponCount= 0;
						}

						if(this.activeName=='first'){
							//平台优惠券
							this.addForm.couponType=2
						}else{
							//店铺优惠券
							this.addForm.couponType=1
						}
						console.log('id:',this.addForm.id)
						if (this.addForm.id) {
							console.log("编辑")
							update(this.addForm).then(res => {
								if (res.data.code == 200) {
									this.addLoad=false
									//前台日期数据格式转化为后台可接收数据格式
									let data = res.data.data
									this.form = data
									console.log(this.form)
									this.$message({
										type: "success",
										message: "编辑优惠券成功!"
									});
									this.onLoad(this.page);
								} else {
									this.$message({
										type: "error",
										message: res.data.message
									});
								}
								setTimeout(()=>{
									this.addLoad=false;
								},1000)
							});
						} else {
							if(this.addForm.couponOrderAmount==undefined){
								this.addForm.couponOrderAmount=0
							}
							add(this.addForm).then(res => {
								if (res.data.code == 200) {
									this.addLoad=false
									//前台日期数据格式转化为后台可接收数据格式
									let data = res.data.data
									this.form = data
									this.$message({
										type: "success",
										message: "新增优惠券成功!"
									});
									this.onLoad(this.page);
								} else {
									this.$message({
										type: "error",
										message: res.data.message
									});
								}
								setTimeout(()=>{
									this.addLoad=false;
								},1000)
							})

						}
						this.addForm = {};
						this.dialogVisible = false;
					// } else {
					// 	return false;
					// }
				// });
			},
			// 新增关闭按钮
			closeDialog(addForm) {
				this.dialogVisible = false;
				this.$refs['addForm'].resetFields();
				this.addLoad=false
			},

			// 点击编辑按钮，调用根据id查询信息按钮，获取信息显示在页面上
			editShow(row) {
				this.title='优惠券编辑'
				this.isView=false
				this.addForm = JSON.parse(JSON.stringify(row))
				/*this.getcouponGetType(row.couponGetType)*/
				if (this.addForm.couponCount == "不限量") {
					this.addForm.couponCount = 0;
				}
				this.dialogVisible = true;
			},
			//优惠券领取类型
			/*getcouponGetType(val){
				// console.log(val)
				for(let obj of this.typeData){
					if(obj.value==val){
						this.addForm.adTypeName=obj.label
						this.addForm.couponGetType=obj.value
						if(obj.value==1){
							this.idTitle='自助领取'
						}else if(obj.value==2){
							this.idTitle='新用户优惠券'
						}else if(obj.value==3){
							this.idTitle='平台手动发放'
						}
					}
				}
			},*/
			// 点击新增优惠券
			addCoupon() {
				this.title='新增优惠券'
				this.isView=false
				this.addForm = {}
				// this.$refs.addForm.resetFields();
				this.dialogVisible = true;
			},
			rowDel(row) {
				this.$confirm("确定将选择数据删除?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						return remove(row.id);
					})
					.then(() => {
						this.$message({
							type: "success",
							message: "操作成功!"
						});
                        if(this.data.length==1){
                            this.page.currentPage-=1
                        }
                        this.onLoad(this.page);
					});
			},
			searchReset() {
				this.query = {};
				this.couponBeginTime ='';
				this.couponEndTime = '';
				if(this.activeName=='first'){
					//平台优惠券
					this.query = {couponType:2};
				}else{
					//店铺优惠券
					this.query = {couponType:1};
				}
				this.onLoad(this.page);
			},
			searchChange(params) {
				this.page.currentPage=1
				console.log("ss")
				if(this.activeName=='first'){
					//平台优惠券
					params.couponType=2
				}else{
					//店铺优惠券
					params.couponType=1
				}
				params.couponBeginTime = this.couponBeginTime
				params.couponEndTime = this.couponEndTime
				this.query = params;
				this.onLoad(this.page, params);
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			refreshChange() {
				this.onLoad(this.page);
				this.onLoad(this.page);
			},
			onLoad(page, params = {}) {
				getList(page.currentPage,page.pageSize,Object.assign(params, this.query)).then(res=>{
					if(this.activeName=='first'){
						//平台优惠券
						this.query.couponType=2
						this.$refs.formRef.init();
						this.$nextTick(() => {
							console.log(this.optionP);
							this.option=this.optionP;
							this.$forceUpdate();
						})
					}else{
						//店铺优惠券
						this.query.couponType=1
						this.$refs.formRef.init();
						this.$nextTick(() => {
							this.option=this.optionD
							this.$forceUpdate();
						})
					}
					this.loading = false;
					const data = res.data;
					this.page.total = data.count;
					for(let item of data.data){
						item.couponBeginTime=item.couponBeginTime.substring(0,10)
						item.couponEndTime=item.couponEndTime.substring(0,10)
						item.couponGetType = item.couponGetType.toString();
						if(item.couponCount == 0){
							item.couponCount = "不限量";
						}
						if(item.couponStatus == 1){
							item.couponStatus = true
						}
						else if(item.couponStatus == 2){
							item.couponStatus = false
						}
					}
					this.data = data.data;
					console.log(this.data);
					this.$forceUpdate();
				})
			},
			//校验开始时间
			checkStartTime () {
				if (this.addForm.couponEndTime) {
					if (new Date(this.addForm.couponBeginTime) > new Date(this.addForm.couponEndTime)) {
						this.$message({
							type: "error",
							message: "开始时间不能大于结束时间"
						});
						this.addForm.couponBeginTime = '';
						return;
					}
				}
			},
			//新增弹框 优惠券使用范围结束时间
			getAddTime() {
				if(this.addForm.couponBeginTime){
					if (this.addForm.couponBeginTime >= this.addForm.couponEndTime) {
						this.$message({
							type: "error",
							message: "结束时间不能小于等于开始时间"
						});
						this.addForm.couponEndTime = ''
					}
				}
			},
			//校验优惠券金额 必须大于0
			checkCouponPrice(){
				if (this.addForm.couponAmount != undefined && this.addForm.couponOrderAmount!=0) {
					if (this.addForm.couponAmount > this.addForm.couponOrderAmount) {
						this.iscouponAmount=true
					}else{
						this.iscouponAmount=false
					}
				}
				if(this.addForm.couponAmount&&this.addForm.couponAmount >10000){
					this.$message({
						type: "error",
						message: "优惠券金额不能大于10000"
					});
					return;
				}
			},
			//校验订单可用金额 可以等于0 等于0时 默认代金券
			checkOrderPrice(){
				if(this.addForm.couponOrderAmount==undefined){
					this.addForm.couponOrderAmount=0
				}
				if (this.addForm.couponAmount != undefined && this.addForm.couponOrderAmount!=0 ) {
					if (this.addForm.couponAmount > this.addForm.couponOrderAmount) {
						this.iscouponAmount=true
					}else{
						this.iscouponAmount=false
					}
				}else if(this.addForm.couponOrderAmount==0 ){
					this.iscouponAmount=false
				}
			},
			rowSwitchChange(row) {
				if (row.couponStatus) {
					updateDisplay(row.id, 1);
				} else {
					updateDisplay(row.id, 2);
				}
			},
		}
	};
</script>

<style scoped>
	@media only screen and (min-width: 992px) {}

	.el-col-md-6 {
		width: 30% !important;
	}
</style>
